package cn.org.timespace.easyframe.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.org.timespace.easyframe.util.report.model.Column;
import cn.org.timespace.easyframe.util.report.model.DataTable;
import cn.org.timespace.easyframe.util.report.model.Report;

public class ReportUtil {

	//单元格key的常量
	public static final String COLUMN_KEY_TABLE_TITLE = "title_";//整个表的标题
	public static final String ROW_COMMON_KEY_HEADER = "row_";//生成行的key前面的部分
	
	//保存列信息的JSON对象的属性名
	public static final String COLUMN_ATTR_TITLE = "title";
	public static final String COLUMN_ATTR_KEY = "key";
	public static final String COLUMN_LABEL_PROVIDER = "lp";
	
	/**
	 *  构建报表表头，headerArr对应的JSON字符中结构如下：
		[
		{title:'房号',key:'room_code',lp:'######'},
		{title:'房型',key:'room_type'},
		{title:'客人姓名',key:'customer_name'},
		{title:'证件类型',key:'certificate_type'},
		{title:'证件编号',key:'certificate_code'},
		{title:'入住时间',key:'check_in_dt'},
		{title:'房价',key:'price'},
		{title:'团队名称',key:'team_name'}
		]
	 */
	public static void buildReportHeader(Report rpt, String jsonArrStr,String... titles) {
		
		try {
			int topicCount = titles.length;
			String title = titles[0];
			
			JSONArray headerArr = new JSONArray(jsonArrStr);
			
			// 第一行是报表名称
			Column titleCol = new Column(rpt);
			titleCol.setTitle(title);
			titleCol.setKey(COLUMN_KEY_TABLE_TITLE+1);

			Column parentCol = titleCol;
			for(int i=1;i<titles.length;i++){
				Column c = new Column(parentCol);
				c.setTitle(titles[i]);
				c.setKey(COLUMN_KEY_TABLE_TITLE+(i+1));
			}
			
			for (int i = 0; i < headerArr.length(); i++) {

				try {
					JSONObject jo = headerArr.getJSONObject(i);

					Column c = new Column(titleCol);
					
					for (Iterator it = jo.keys(); it.hasNext();) {

						String key = (String) it.next();
						String value = jo.getString(key);
						
						if(key.equalsIgnoreCase(COLUMN_ATTR_TITLE)){
							c.setTitle(value);
						}else if(key.equalsIgnoreCase(COLUMN_ATTR_KEY)){
							c.setKey(value.toLowerCase());
						}
//						System.out.println(key + "--" + value);
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 构建报表表体
	 * 
	 */
	public static void buildReportData(Report rpt,
			List<Map<String, Object>> dataList) {
		
		DataTable dt = rpt.getDataTable();
		int i = 0;
		
		for(Map<String, Object> map : dataList){
			i++;
			String row_key = ROW_COMMON_KEY_HEADER+i;
			Set<String> keySet = map.keySet();
			for(String colKey:keySet){
				String lowColKey = colKey.toLowerCase();
				Column tmp = null;
				try{
					tmp = rpt.getColumn(lowColKey);
					if(tmp==null)
						continue;
				}catch (Exception e) {
					continue;
				}
				dt.setData(row_key, lowColKey, map.get(colKey));
			}
		}
	}
	
}
